package com.liufh.filter;

//import com.liufh.common.ResultData;
import com.liufh.service.UserServiceFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义全局过滤器，进⾏token的验证，⽤户微服务和验证码微服务的请求不过滤
 *
 * @author: liufh
 * @create: 2022-01-26 11:52
 */
@Slf4j
//@Component // 让容器扫描到，等同于注册了
public class TokenFilter implements GlobalFilter, Ordered {

    @Autowired
    private UserServiceFeignClient userServiceFeignClient;

    /**
     * 过滤器核⼼⽅法【待测试】
     *
     * @param exchange 封装了request和response对象的上下⽂
     * @param chain    ⽹关过滤器链（包含全局过滤器和单路由过滤器）
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 要先获取请求路径，排除部分接口后再过滤


        // 获取token，调用用户微服务进行校验
//        ResultData info = userServiceFeignClient.info();



        // 思路：获取客户端ip，判断是否在⿊名单中，在的话就拒绝访问，不在的话就放⾏
        // 从上下⽂中取出request和response对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        // 从request对象中获取客户端ip
        String clientIp = request.getRemoteAddress().getHostString();
        // 拿着clientIp去⿊名单中查询，存在的话就决绝访问
//        if (blackList.contains(clientIp)) {
//            // 决绝访问，返回
//            response.setStatusCode(HttpStatus.UNAUTHORIZED); // 状态码
//            log.debug("=====>IP:" + clientIp + " 在⿊名单中，将被拒绝访问！");
//            String data = "Request be denied!";
//            DataBuffer wrap = response.bufferFactory().wrap(data.getBytes());
//            return response.writeWith(Mono.just(wrap));
//        }
        // 合法请求，放⾏，执⾏后续的过滤器
        return chain.filter(exchange);
    }

    /**
     * 返回值表示当前过滤器的顺序(优先级)，数值越⼩，优先级越⾼
     */
    @Override
    public int getOrder() {
        return 0;
    }

}
